//
//  ChooseServerTypeViewController.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/13.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "ChooseServerTypeViewController.h"
#import "ChooseServerTypeCollectionReusableView.h"
#import "ChooseServerTypeCollectionViewCell.h"
#import "SendServerDetailViewController.h"

@interface ChooseServerTypeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *shopingCollectionView;
@property (nonatomic, strong) NSMutableArray *datamuArray;
@end

@implementation ChooseServerTypeViewController
/**
获取服务二三级分类接口
 */
- (void)getServersList {
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=servicetwocat" parameters:@{@"uid":[UserModel sharedInstanced].userId,@"token":[UserModel sharedInstanced].token,@"parentcat":self.dataDic[@"catid"]} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
//            ShowMessage(@"操作成功");
            self.datamuArray = responseObject[@"data"];
            [self.shopingCollectionView reloadData];
        }else{
            ShowMessage(@"操作失败");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"发布服务";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getServersList];
    [self.view addSubview:self.shopingCollectionView];
    __weak typeof(self)weakself = self;
    [self.shopingCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.view);
        make.left.equalTo(weakself.view.mas_left).offset(10 / WIDTH_6S_SCALE);
        make.right.equalTo(weakself.view.mas_right).offset(-10 / WIDTH_6S_SCALE);
        make.height.mas_equalTo(SCREEN_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight);
    }];
}
#pragma mark - UICollectionViewDelegate and UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.datamuArray.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *array = self.datamuArray[section][@"level"];
    return array.count;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SendServerDetailViewController *VC = [[SendServerDetailViewController alloc]init];
    VC.isChooseBigType = YES;
    VC.firstDataDic = self.dataDic;
    VC.secondDataDic = self.datamuArray[indexPath.section][@"level"][indexPath.row];
    [self.navigationController pushViewController:VC animated:NO];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseServerTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ChooseServerTypeCollectionViewCell class]) forIndexPath:indexPath];
    cell.dataDic = self.datamuArray[indexPath.section][@"level"][indexPath.row];
    return cell;
}
// 设置头尾部内容(多数情况可以不用)
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView =nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        //定制头部视图的内容
        ChooseServerTypeCollectionReusableView *headerV = (ChooseServerTypeCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        
        headerV.titleLabel.text = self.datamuArray[indexPath.section][@"catname"];
//        if (indexPath.section == 0) {
//            headerV.titleLabel.text = @"家电清洁";
//        }
//        if (indexPath.section == 1) {
//            headerV.titleLabel.text = @"家电换新";
//        }
//        if (indexPath.section == 2) {
//            headerV.titleLabel.text = @"家电修理";
//        }
        reusableView = headerV;
    }
    return reusableView;
}
- (UICollectionView *)shopingCollectionView
{
    if (!_shopingCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 30 / WIDTH_6S_SCALE) / 3,40 / HEIGHT_6S_SCALE);
        flowLayout.minimumLineSpacing = 5 / WIDTH_6S_SCALE;
        flowLayout.minimumInteritemSpacing = 5 / WIDTH_6S_SCALE; // 横向间距离
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        // 设置头部并给定大小(多数情况可以不用)
        [flowLayout setHeaderReferenceSize:CGSizeMake(SCREEN_HEIGHT,40 / HEIGHT_6S_SCALE)];
        
        _shopingCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _shopingCollectionView.backgroundColor = [UIColor clearColor];
        _shopingCollectionView.bounces = YES;
        _shopingCollectionView.delegate = self;
        _shopingCollectionView.dataSource = self;
        _shopingCollectionView.showsVerticalScrollIndicator = NO;
        _shopingCollectionView.showsHorizontalScrollIndicator = NO;
        _shopingCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [_shopingCollectionView registerClass:[ChooseServerTypeCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ChooseServerTypeCollectionViewCell class])];
        // 注册头部视图 (多数情况可以不用)
        [_shopingCollectionView registerClass:[ChooseServerTypeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    }
    
    return _shopingCollectionView;
}
- (NSMutableArray *)datamuArray
{
    if (!_datamuArray) {
        _datamuArray = @[].mutableCopy;
    }
    return _datamuArray;
}
@end
